// GUI COMPONENT - SCROLLED LOG.

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;


public class Log extends JScrollPane {
	private static final long serialVersionUID = -1550337135732026406L;
	
	private JList<String> content;
	private DefaultListModel<String> listModel;

	public Log(String title) {
		setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
		setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
		
		setBorder(BorderFactory.createTitledBorder(title));
		
		listModel = new DefaultListModel<String>();
        content = new JList<String>(listModel);
        setViewportView(content);
	}
	
	public void addItem(final String newItem) {
		SwingUtilities.invokeLater(new Runnable() { 
			public void run() {
				DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
				Date date = new Date();
				listModel.addElement(dateFormat.format(date) + " - " + newItem);
				content.ensureIndexIsVisible(listModel.getSize()-1);
			}
		});
	}
	

}
